﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Bussines;



namespace ownx
{
    public partial class _Default : System.Web.UI.Page
    {       
        clUser _clUser;
        clFIGYELEM _clFigyelem;
        string ConSTR = @"server=ERNIE-HOME\SQLEXPRESS;database=OWNX;uid=sa;password=x";
        //string ConSTR = @"server=localhost;database=OWNX;uid=sa;password=";
        clFIELDINFO_LIST FieldInfo;
        clCalendar _clCalendar;

        protected _Default()
        {
            FieldInfo = new clFIELDINFO_LIST(ConSTR);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["CONSTR"] = ConSTR;
            Session["FIELDINFO"] = FieldInfo;
            Session["DEFAULT_BACK_URL"] = "wfDefault.aspx";
            Session["DEFAULT_UPLOADED_FILE_PATH"] = @"/upload";
            Session["DEFAULT_DELETED_FILE_PATH"] = @"/upload/deleted";
            Session["FILTER"] = "";


            TBLObj blObj = new TBLObj(-1, ConSTR, FieldInfo);
      
            _clUser = new clUser(blObj);


            if (Session["UserName"] != null)
            {
                
                blObj.USR_ID = (int)Session["USR_ID"];
                if (Session["UserName"].ToString() == "SYS") blObj.USR_ID = -666;
                Session["BL"] = blObj;
                
                _clFigyelem = new clFIGYELEM(blObj);

                Login1.Visible = false;
                lUserName.Text = Session["UserName"].ToString() + "   " + Session["USR_ID"].ToString();
                _clFigyelem.ExecSP_FIGYELEM((int)Session["USR_ID"], DateTime.Now );
                FillFigyelem();
                if (!(Page.IsPostBack))
                {
                    calMEGBASZ.SelectedDate = DateTime.Today;
                    _clFigyelem.FilldsFN_FIGYELEM((int)Session["USR_ID"], calMEGBASZ.SelectedDate);
                    gvFN_Figyelem.DataSource = _clFigyelem.dsFN_FIGYELEM;
                    gvFN_Figyelem.DataBind();
                    gvFN_Figyelem.Visible = true;
                    calMEGBASZ.Visible = true;
                }
            }            
            
            lUserName.Visible = (Session["UserName"] != null);
            lbLogout.Visible = (Session["UserName"] != null);
            lbChangePw.Visible = (Session["UserName"] != null);

            SetButtons(Session["UserName"] != null, blObj);
            
            //naptarban bejelolni a napokat, ahol van esemeny
            //calMEGBASZ.SelectedDates.Clear();
            _clCalendar = new clCalendar(blObj); 
            DataSet ds = new DataSet();
            ds = _clCalendar.GetData( "TRV_CREATED_BY = " + blObj.USR_ID.ToString() );
            for (int i = 0; i < ds.Tables["TERVEK"].Rows.Count; ++i)
            {
                calMEGBASZ.SelectedDates.Add(Convert.ToDateTime((DateTime)ds.Tables["TERVEK"].Rows[i]["TRV_IDOPONT"]));
            } 
        }
        
        private void SetButtons(bool UserOk, TBLObj iblObj)
        {
            List<Button> Buttons = new List<Button>();
            int X = 0;

            lbUsers.Visible = (UserOk && iblObj.UserRights.GetRight(5));
            lbCalendar.Visible = (UserOk && iblObj.UserRights.GetRight(2));
            lbParnterChoice.Visible = (UserOk && iblObj.UserRights.GetRight(1));
            lbPalyazok.Visible = (UserOk && iblObj.UserRights.GetRight(3));
            lbTorzsek.Visible = (UserOk && iblObj.UserRights.GetRight(4));
            lbKonstrukcio.Visible = (UserOk && iblObj.UserRights.GetRight(6));

            if (lbUsers.Visible)
            {
                Buttons.Add(lbUsers);
            }
            if (lbCalendar.Visible)
            {
                Buttons.Add(lbCalendar);
            }
            if (lbParnterChoice.Visible)
            {
                Buttons.Add(lbParnterChoice);
            }
            if (lbPalyazok.Visible)
            {
                Buttons.Add(lbPalyazok);
            }
            if (lbTorzsek.Visible)
            {
                Buttons.Add(lbTorzsek);
            }
            if (lbKonstrukcio.Visible)
            {
                Buttons.Add(lbKonstrukcio);
            }

            foreach (Button B in Buttons)
            {                
                tbButtons.Rows[0].Cells[X].Controls.Add(B);
                X++;
            }

        }

        private void FillFigyelem()
        {
            int db = 0;
            db = _clFigyelem.FilldsFIGYELEM((int)Session["USR_ID"]);

            if (db > 0)
            {
                lFigyelem.Visible = true;
                gvFigyelem.Visible = true;
                gvFigyelem.DataSource = _clFigyelem.dsFIGYELEM;
                gvFigyelem.DataBind();
            }
            else
            {
                lFigyelem.Visible = false;
                gvFigyelem.Visible = false;
            }
/*
            _clFigyelem.FilldsFN_FIGYELEM((int)Session["USR_ID"], calMEGBASZ.SelectedDate );
            gvFN_Figyelem.DataSource = _clFigyelem.dsFN_FIGYELEM ;
            gvFN_Figyelem.DataBind();
            gvFN_Figyelem.Visible = true;
            calMEGBASZ.Visible = true;
 */
        }


        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            bool Jo = false;

            
            Jo = (!(_clUser.IsUserLocked(Login1.UserName)));

            

            if (!(Jo))
            {
                Login1.FailureText = "Felhasználó zárolva, túl sok sikertelen kisérlet miatt!";
            }
            else
            {
                Login1.FailureText = "Felhasználónév vagy jelszó nem megfelől!";
                Jo = _clUser.AuthenticateUser(Login1.UserName, Login1.Password);
                if (Jo)
                {
                    Session["USR_ID"] = _clUser.GetUSR_ID();                    
                }
            }
            e.Authenticated = Jo;
        }

        

        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            Session["UserName"] = Login1.UserName  ;
            Response.Redirect("wfDefault.aspx");
        }

      

        protected void LinkButton1_Click(object sender, EventArgs e)
        {            
            Session["UserName"] = null;
            Response.Redirect("wfDefault.aspx");
        }

        protected void lbChangePw_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfChangePW.aspx");
        }

        protected void lbUsers_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfUsers.aspx");
        }        
               
        protected void lbStock_Click(object sender, EventArgs e)
        {
            Session["SHOW_STOCK_BUTTONS"] = "1";
            Session["TABLE"] = "ORSZAG";
            Session["CAPTION"] = "Országok";
            Response.Redirect("wfStock.aspx");
        }

        protected void lbCalendar_Click(object sender, EventArgs e)
        {            
            Response.Redirect("wfCalendar.aspx");
        }        

        protected void lbParnterChoice_Click(object sender, EventArgs e)
        {            
            Response.Redirect("wfPartnerChoice.aspx");            
        }

        protected void gvFigyelem_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int r = Convert.ToInt32( e.CommandArgument ); 
            int iFGY_ID = -1 ;
            if (e.CommandName == "ELFOGAD")
            {                
                iFGY_ID = (int)_clFigyelem.dsFIGYELEM.Tables["FIGYELEM"].Rows[r]["FGY_ID"];
                _clFigyelem.SetOk(iFGY_ID);

                FillFigyelem();
            }

        }
        
        protected void bntDisp_Click(object sender, EventArgs e)
        {
            
        }

        protected void gvFN_Figyelem_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void lbPalyazok_Click(object sender, EventArgs e)
        {
            Session["SHOW_STOCK_BUTTONS"] = "0";
            Session["TABLE"] = "PALYAZOK";
            Session["CAPTION"] = "Pályázók";
            Response.Redirect("wfStock.aspx");
        }

        protected void lbKonstrukcio_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfKonstrukcio.aspx");            
        }

        protected void calMEGBASZ_SelectionChanged(object sender, EventArgs e)
        {
            Session["SELECTED_DATE"] = calMEGBASZ.SelectedDate;
            _clFigyelem.ExecSP_FIGYELEM((int)Session["USR_ID"], calMEGBASZ.SelectedDate);
            FillFigyelem();

            _clFigyelem.FilldsFN_FIGYELEM((int)Session["USR_ID"], calMEGBASZ.SelectedDate);
            gvFN_Figyelem.DataSource = _clFigyelem.dsFN_FIGYELEM;
            gvFN_Figyelem.DataBind();
            gvFN_Figyelem.Visible = true;

            TBLObj blObj = new TBLObj(-1, ConSTR, FieldInfo);
            blObj.USR_ID = (int)Session["USR_ID"];
            _clCalendar = new clCalendar(blObj);
            DataSet ds = new DataSet();
            ds = _clCalendar.GetData("TRV_CREATED_BY = " + blObj.USR_ID.ToString());
            for (int i = 0; i < ds.Tables["TERVEK"].Rows.Count; ++i)
            {
                calMEGBASZ.SelectedDates.Add(Convert.ToDateTime((DateTime)ds.Tables["TERVEK"].Rows[i]["TRV_IDOPONT"]));
            }
            
        }

         

    }
}
